<?php
/**
 * 
 * Save a new project
 * 
 * This file saves the new project under the user area
 *
 * 
 * @copyright 2007 Loughborough University
 * @license http://www.gnu.org/licenses/gpl.txt
 * @version 0.0.0.1
 * @since 13 Oct 2009
 * 
 * @todo put in some validation of the information that is included from the server.
 * 
 */
 
 function logit($input) {
     $handle = fopen('__log.txt', "a");
     fwrite($handle, var_export($input, true) . "\n");
 }

 logit("---------------------LOGGING A NEW PROJECT--------------------"); 
 
 
 //get the xml for the new project from the post.
 $new_project = $_POST["myxml"];
 
 logit("Project XML to save - - - " . $new_project . " - - - " . date());
 
 echo $new_project;
 
 //we need to write the xml to the server after extracting the project name.
 
 //extract the name
 $doc = new DOMDocument();
 $doc->loadXML($new_project);
 $project_name = $doc->getElementsByTagName( "name" );
 
 echo $new_project;
 
 //format the name -> remove capitals and any other characters not needed
  
 $project_name = $project_name->item(0)->nodeValue;
 $project_name = trim($project_name);
 $project_name = ereg_replace("[^A-Za-z0-9]", "", $project_name);
 
 echo "<br><br>" . $project_name;
 
 //create a directory for the project with the refined name
 $path = dirname( __FILE__ );
 $fullPath = $path . '/../users/test' . '/' . $project_name;				//../users/test will be replaced by the username of the person using the tool!
 mkdir($fullPath);
 
 //while we are at it create the assests folder to contain all the uploaded components
 $assets = $fullPath . '/assets/';
 mkdir($assets);
 
 //save the XML into the folder as the project.xml
 $doc->save($fullPath . "/project.xml");
 
 logit("----------------------------------- END LOGGING A NEW PROJECT ----------------------------------------------");

?>